package com.general.interceptor.jdk;

import com.general.commons.lang.util.ClassLoaderUtils;
import com.general.interceptor.ComponentEnhancer;

import java.lang.reflect.Proxy;

/**
 * @description: JdkProxyComponentEnhancer
 * @author: general
 * @version: 1.0
 * @create: 2021-10-26 20:11
 **/
public class JdkProxyComponentEnhancer implements ComponentEnhancer {

    @Override
    public <T> T enhance(T source, Class<? super T> componentClass,
                         Object... defaultInterceptors) {
        ClassLoader classLoader = ClassLoaderUtils.getClassLoader();
        // ClassLoader loader,
        // Class<?>[] interfaces,
        // InvocationHandler h
        return (T)Proxy.newProxyInstance(
                classLoader,
                source.getClass().getInterfaces(), new InvocationHandlerAdapter(source, defaultInterceptors));
    }
}
